package com.jinher.lib
{
	import air.net.URLMonitor;
	
	import com.adobe.serialization.json.*;
	import com.jinher.lib.Const.LnConst;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;

	/**
	 * 创建时间：2011-10-14
	 * 创建人：王曙光
	 * 功能描述：网络访问相关
	 */
	public final class Net
	{
		//网络回传数据
		public var data:Object=new Object();
		//网络监听，静态变量
		public static var urlMonitor:URLMonitor=null;
		//添加事件，当网络状态发生改变时触发
		public static var Event_Net_StatusChange:String="com.jinher.Event_Net_StatusChange";
		//添加事件，当POST数据返回时触发
		public static var Event_Net_PostBack:String="com.jinher.Event_Net_PostBack";
		//添加事件，当POST数据请求出问题
		public static var Event_Net_PostError:String="com.jinher.Event_Net_PostError";
		//事件触发器
		private var _eventDispatcher:EventDispatcher=null;
		
		//url loader
		private var loader:URLLoader;

		//处理自定义事件
		private function doAction(eventName:String):void
		{
			var event:Event=new Event(eventName);
			_eventDispatcher.dispatchEvent(event);
		}

		//网络状态改变时响应该事件
		private function netWorkStatusHandle(e:StatusEvent):void
		{
			doAction(Event_Net_StatusChange);
		}

		//POST数据成功返回时
		private function loader_complete(e:Event):void
		{
			data=JSON.decode(loader.data);
			if (String(data.Error) == "0")
				doAction(Event_Net_PostBack);
			else
			{
				data = data.ErrorMessage;
				doAction(Event_Net_PostError);
			}
			removeLoaderEvent();
		}

		//连接打开
		private function loader_open(e:Event):void
		{
			//trace("Event.OPEN");
			//trace("读取了的字节 : " + loader.bytesLoaded);
		}

		//状态改变
		private function loader_httpStatus(e:HTTPStatusEvent):void
		{
			//trace("HTTPStatusEvent.HTTP_STATUS");
			//trace("HTTP 状态代码 : " + e.state);
		}

		//进度改变
		private function loader_progress(e:ProgressEvent):void
		{
			//trace("ProgressEvent.PROGRESS");
			//trace("读取了的字节 : " + loader.bytesLoaded);
			//trace("文件总字节 : " + loader.bytesTotal);
		}

		//权限受限
		private function loader_security(e:SecurityErrorEvent):void
		{
			//trace("SecurityErrorEvent.SECURITY_ERROR");
			data=LnConst.NetSecurityError;
			doAction(Event_Net_PostError);
			removeLoaderEvent();
		}

		//io错误
		private function loader_ioError(e:IOErrorEvent):void
		{
			//trace("IOErrorEvent.IO_ERROR");
			data=LnConst.NetIOError;
			doAction(Event_Net_PostError);
			removeLoaderEvent();
		}

		//移除loader 所有注册事件
		private function removeLoaderEvent():void
		{
			loader.removeEventListener(Event.COMPLETE, loader_complete);
			loader.removeEventListener(Event.OPEN, loader_open);
			loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, loader_httpStatus);
			loader.removeEventListener(ProgressEvent.PROGRESS, loader_progress);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_security);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, loader_ioError);
		}

		//构造函数
		public function Net()
		{
			_eventDispatcher=new EventDispatcher();
		}

		/**
		 * 事件注册函数
		 */
		public function addEventListener(type:String, listener:Function):void
		{
			_eventDispatcher.addEventListener(type, listener);

		}

		/**
		 * 移除事件注册
		 */
		public function removeEventListener(type:String, listener:Function):void
		{
			_eventDispatcher.removeEventListener(type, listener);
		}

		/**
		 * 网络监听，当网络状态发生改变时触发注册的事件
		 */
		public function checkNet(url:String):void
		{
			//创建一个联接，使用HEAD表示不需要加载玩全部的页面。
			var request:URLRequest=new URLRequest(url);
			request.method=URLRequestMethod.HEAD;
			//用url创建监测对象，每3秒检查一次网络状态，如果网络状态发生变化，将结果返回到netWorkStatusHandle()
			urlMonitor=new URLMonitor(request);
			urlMonitor.pollInterval=3000;
			urlMonitor.addEventListener(StatusEvent.STATUS, netWorkStatusHandle);
			urlMonitor.start();
		}

		/**
		 * 以POST方式请求网络数据 
		 */		
		public function getJSON(url:String, variables:String):void
		{
			var request:URLRequest=new URLRequest();
			request.url=url;
			//设置请求方式
			request.method=URLRequestMethod.POST;
			
			//发送数据
			request.data=variables;
			//设置参数的传递方式
			request.contentType="application/json";
			loader=new URLLoader();
			//回传数据格式
			loader.dataFormat=flash.net.URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, loader_complete);
			loader.addEventListener(Event.OPEN, loader_open);
			loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, loader_httpStatus);
			loader.addEventListener(ProgressEvent.PROGRESS, loader_progress);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_security);
			loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioError);
			loader.load(request);
		}
	}
}